//
//  GYDetailsFootView.m
//  Visitor
//
//  Created by x on 2017/9/15.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "GYDetailsFootView.h"
#import "UILabel+GYLable.h"
#import "UIButton+GYButton.h"

@interface GYDetailsFootView ()<UITextViewDelegate>

/** 标记lable */
@property (nonatomic, weak) UILabel *markLable;

/** 确定按钮 */
@property (nonatomic, weak) UIButton *determineBtu;

/** 处理事由 */
@property (nonatomic, weak) UITextView *causeTextView;

/** 提示信息lable */
@property (nonatomic, weak) UILabel *markLbale;
@end

@implementation GYDetailsFootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1.初始化子控件
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    //1.创建子控件
    UITextView *textView = [[UITextView alloc] init];
    textView.translatesAutoresizingMaskIntoConstraints = NO;
    textView.delegate = self;
    self.causeTextView = textView;
    [self addSubview:textView];
    
    UILabel *markLable = [UILabel lableWithMark:@"请输入备注信息" titleColor:RGBColor(0XCCCCCC) backgroundColor:[UIColor clearColor] font:16];
    self.markLable = markLable;
    [textView addSubview:markLable];
    
    UIButton *determineBtu = [UIButton buttonWith:@"确定" titleColor:RGBColor(0XFFFFFF) borderColor:[UIColor clearColor] backgroundColor:RGBColor(0X3A9DF1) tag:0 target:self action:@selector(determineBtuClick)];
    self.determineBtu = determineBtu;
    [self addSubview:determineBtu];
    
    //2.布局子控件
    NSDictionary *dic = @{@"textView":textView,@"markLable":markLable,@"determineBtu":determineBtu};
    NSDictionary *metrics = @{@"btuW":@(WIDTH-24)};
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[textView]-0-|" options:0 metrics:nil views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[textView(80)]" options:0 metrics:nil views:dic]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[markLable]" options:0 metrics:nil views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[markLable]" options:0 metrics:nil views:dic]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[determineBtu(btuW)]" options:0 metrics:metrics views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[textView]-60-[determineBtu(44)]" options:0 metrics:nil views:dic]];
}

- (CGFloat)footViewHeight {
    [self layoutIfNeeded];
    
    return CGRectGetMaxY(self.determineBtu.frame) + 15;
}
/**
 确定按钮点击事件
 */
- (void)determineBtuClick {
    [self.causeTextView resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(uploadVisitorHandleState:)]) {
        [self.delegate uploadVisitorHandleState:self.causeTextView.text];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.causeTextView resignFirstResponder];
}
- (void)setModel:(GYVisitor *)model {
    _model = model;
    
    //设置按钮是否吟唱
    self.causeTextView.editable = model.result == 0 ? NO : YES ;
    self.determineBtu.hidden = model.result == 0 ? NO : YES ;
}

# pragma mark -- <UITextViewDelegate>

- (void)textViewDidChange:(UITextView *)textView {
    self.markLable.hidden = textView.hasText;
}









@end
